﻿using EVEBatteryCheck.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TYBase;

namespace EVEBatteryCheck.Comm {
    public class TrayDataHelper {
        public static bool ParseBatteryData(string filePath, out TrayFromPreCharge trayFromPreCharge) {
            trayFromPreCharge = null;
            try {
                var lines = File.ReadAllLines(filePath);
                trayFromPreCharge = new TrayFromPreCharge {
                    Cells = new List<CellData>()
                };

                foreach (var line in lines) {
                    if (line.Null()) {
                        continue;
                    }
                    var parts = line.Split(',');

                    if (line.StartsWith("Tray ID")) {
                        trayFromPreCharge.TrayID = parts[1].Trim();
                    }
                    else if (line.StartsWith("Cell Type")) {
                        trayFromPreCharge.CellType = parts[1].Trim();
                    }
                    else if (line.StartsWith("CH")) {
                        // 跳过标题行
                        continue;
                    }
                    else if (parts.Length == 5) // 确保这一行包含所有需要的数据
                    {
                        trayFromPreCharge.Cells.Add(new CellData {
                            CH = int.Parse(parts[0].Trim()),
                            LotId = parts[1].Trim(),
                            Serial = int.Parse(parts[2].Trim()),
                            IR = double.Parse(parts[3].Trim()),
                            OCV = double.Parse(parts[4].Trim())
                        });
                    }
                }

                return true;
            }
            catch (Exception ex) {
                Log.E("ParseBatteryData Error", ex);
                return false;
            }
        }
    }
}
